import pandas as pd

bookdata=pd.read_csv('../dataset/book/book_tags.csv', encoding='gbk')
tagdata=pd.read_csv('../dataset/book/tag.csv')

def marge(bookdata,tagdata):
    bookid=[]
    bookname=[]
    bookauthor=[]
    booktime=[]
    bookkpublisher=[]
    bookmoney=[]
    booktag=[]

    tagid=[]
    tagname=[]
    tagpreid=[]

    for i in range(len(tagdata)):
        item=tagdata.loc[i]
        tagid.append(item[1])
        tagname.append(item[2])
        tagpreid.append(item[3])

    for i in range(0,len(bookdata),9):
        item=bookdata.loc[i]
        bookid.append(int(i/9)+1)
        bookname.append(item[0])
        bookauthor.append(item[1])
        booktime.append(item[2].strip())
        bookkpublisher.append(item[3].strip())
        bookmoney.append(item[4])
        itemtag=item[5].split(",")
        string=""
        for index,j in enumerate(itemtag):
            if len(itemtag)-1==index:
                string+=str(tagid[tagname.index(j)])
            else:
                string+=str(tagid[tagname.index(j)])+"-"
        booktag.append(string)





    df=pd.DataFrame({'book_id':bookid,'book_name':bookname,'book_author':bookauthor,'book_time':booktime,'book_publisher':bookkpublisher,'book_money':bookmoney,'book_tag':booktag})
    return df

if __name__ == '__main__':
    marge(bookdata,tagdata).to_csv('../dataset/book/book.csv')